<?php

namespace App\Admin\Controllers;

use App\Admin\Charts\OrderBar;
use App\Admin\Charts\UserBar;
use App\Admin\Metrics\Examples;
use App\Http\Controllers\Controller;
use Dcat\Admin\Http\Controllers\Dashboard;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Box;
use Dcat\Admin\Widgets\Dropdown;
use Illuminate\Support\Facades\DB;

class HomeController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('首页')
            ->description('首页')
            ->body(function (Row $row) {
                $row->column(12, function (Column $column) {

                    $column->row(function (Row $row){
                        $userCount = DB::table('users')->count('id');
                        $userBox = Box::make('总用户',"<span  style='text-align: center;display:block;'>$userCount</span>");
                        $row->column(3,$userBox);

                        $articleCount = DB::table('articles')
                                         ->where('type',1)
                                         ->count('id');
                        $articleBox = Box::make('总文章',"<span  style='text-align: center;display:block;'>$articleCount</span>");
                        $row->column(3,$articleBox);

                        $adCount = DB::table('ads')
                            ->where('status','>',1)
                            ->sum('id');

                        $adBox = Box::make('总广告',"<span  style='text-align: center;display:block;'>$adCount</span>");
                        $row->column(3,$adBox);

                        $courseCount = DB::table('courses')
                            ->count('id');
                        $courseBox = Box::make('总课程',"<span  style='text-align: center;display:block;'>$courseCount</span>");
                        $row->column(3,$courseBox);
                    });
                });


                $row->column(12, function (Column $column) {

                    $column->row(function (Row $row){
                        $userAuthAuditCount = DB::table('user_auths')

                                ->where('audit_status',1)->count('id');
                        $userAuthAuditBox = Box::make('个人实名认证待审核数量',"<a href='/admin/user/auth'  style='color:red; text-align: center;display:block;'>$userAuthAuditCount</a>");
                        $row->column(3,$userAuthAuditBox);


                        $firmAuditCount = DB::table('firms')
                                        ->where('audit_status',1)
                                        ->count('id');
                        $firmAuditBox = Box::make('企业认证待审核数量',"<a href='/admin/firm'  style='color:red; text-align: center;display:block;'>$firmAuditCount</a>");
                        $row->column(3,$firmAuditBox);


                        $brandAuditCount = DB::table('firm_brands')
                            ->where('audit_status',1)
                            ->count('id');
                        $brandAuditBox = Box::make('品牌认证待审核数量',"<a href='/admin/firm/brand'  style='color:red; text-align: center;display:block;'>$brandAuditCount</a>");
                        $row->column(3,$brandAuditBox);

                        $articleAuditCount = DB::table('articles')
                                        ->where('type',1)
                                        ->where('audit_status',1)
                                        ->count('id');
                        $articleAuditBox = Box::make('文章待审核数量',"<a href='/admin/article'  style='color:red;text-align: center;display:block;'>$articleAuditCount</a>");
                        $row->column(3,$articleAuditBox);

                        $adAuditCount = DB::table('ads')
                            ->where('audit_status',1)
                            ->where('status','>',1)
                            ->sum('id');

                        $adAuditBox = Box::make('广告待审核数量',"<a href='/admin/ad'  style='color:red;text-align: center;display:block;'>$adAuditCount</a>");
                        $row->column(3,$adAuditBox);

                        $courseAuditCount = DB::table('courses')
                            ->where('audit_status',1)
                            ->count('id');
                        $courseAuditBox = Box::make('课程待审核数量',"<a href='/admin/course'  style='color:red;text-align: center;display:block;'>$courseAuditCount</a>");
                        $row->column(3,$courseAuditBox);
                    });
                });


                $row->column(12, function (Column $column) {
                    $menu = [
                        '7'  => '最近7天',
                        '30' => '最近30天',
                    ];

                    $dropdown = Dropdown::make($menu)
                        ->button(current($menu))
                        ->click()
                        ->map(function ($v, $k) {
                            // 此处设置的 data-xxx 属性会作为post数据发送到后端api
                            return "<a class='switch-user-bar' data-option='{$k}'>{$v}</a>";
                        });

                    $bar = UserBar::make()
                        ->fetching('$("#user").loading()') // 设置loading效果
                        ->fetched('$("#user").loading(false)') // 移除loading效果
                        ->click('.switch-user-bar');

                    $box = Box::make('用户数量', $bar)
                        ->id('user') // 设置盒子的ID
                        ->tool($dropdown);

                    $column->row($box);
                });

//                $row->column(12, function (Column $column) {
//                    $menu = [
//                        '7'  => '最近7天',
//                        '30' => '最近30天',
//                    ];
//
//                    $dropdown = Dropdown::make($menu)
//                        ->button(current($menu))
//                        ->click()
//                        ->map(function ($v, $k) {
//                            // 此处设置的 data-xxx 属性会作为post数据发送到后端api
//                            return "<a class='switch-order-bar' data-option='{$k}'>{$v}</a>";
//                        });
//
//                    $bar = OrderBar::make()
//                        ->fetching('$("#order").loading()') // 设置loading效果
//                        ->fetched('$("#order").loading(false)') // 移除loading效果
//                        ->click('.switch-order-bar');
//
//                    $box = Box::make('订单数量', $bar)
//                        ->id('order') // 设置盒子的ID
//                        ->tool($dropdown);
//
//                    $column->row($box);
//                });
            });
    }
}
